/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlparser;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import socofwebget.Ficheiros;

/**
 *
 * @author Helder
 */
public class UrlParser {

    public UrlParser() {
    }
    private String type;

    /**
     * get's the values for HREF and SRC
     *
     * @param filePath path of the html file to perse
     * @param type type of tag ti look (img, image, a, link, all)
     *
     * @return
     */
    public ArrayList<String> getUrl(Ficheiros dbObj, String type) {
        String filePath = dbObj.getCaminholocal() + dbObj.getFicheiro();
        dbObj.setEstado(dbObj.DB_STATE_PARSING);

        final ArrayList<String> attributeList = new ArrayList<String>();
        final ArrayList<String> ImgList = new ArrayList<String>();
        final ArrayList<String> returnValue;
        ParserDelegator parserDelegator = new ParserDelegator();

        HTMLEditorKit.ParserCallback parserCallback = new HTMLEditorKit.ParserCallback() {
            public void handleText(final char[] data, final int pos) {
            }

            public void handleStartTag(HTML.Tag tag, MutableAttributeSet attribute, int pos) {
                //Aqui captura os links
                if (tag == HTML.Tag.A) {
                    String address = (String) attribute.getAttribute(HTML.Attribute.HREF);
                    attributeList.add(address);
                }
            }

            public void handleEndTag(HTML.Tag t, final int pos) {
            }

            public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet attribute, final int pos) {
                //Aqui captura as imagens
                if (tag == HTML.Tag.IMG) {
                    String address = (String) attribute.getAttribute(HTML.Attribute.SRC);
                    ImgList.add(address);
                }
            }

            public void handleComment(final char[] data, final int pos) {
            }

            public void handleError(final java.lang.String errMsg, final int pos) {
            }
        };
        try {
            parserDelegator.parse(new FileReader(filePath), parserCallback, false);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        type = type.toLowerCase();
        switch (type) {
            case "img":
            case "image":
                returnValue = ImgList;
                break;
            case "a":
            case "link":
                returnValue = attributeList;
                break;
            case "all":
            default:
                for (String img : ImgList) {
                    attributeList.add(img);
                }
                returnValue = attributeList;
                break;
        }

        boolean parsedResult;
        LinkChanger web2Local = new LinkChanger();
        try {
            parsedResult = web2Local.linkChanger2local(dbObj);
            dbObj.setEstado(dbObj.DB_STATE_COMPLETE);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        return returnValue;

//
//        //verify what to return if fails to set DB
//        return new ArrayList<>();
    }
}
